package de.hochschuletrier.bachelor.antfarm.world;

import java.awt.Point;

public class Direction {
	public static final int NORTH = 0;
	public static final int NORTHEAST = 1;
	public static final int EAST = 2;
	public static final int SOUTHEAST = 3;
	public static final int SOUTH = 4;
	public static final int SOUTHWEST = 5;
	public static final int WEST = 6;
	public static final int NORTHWEST = 7;

	public static int getDirection(int x, int y) {
		// Auf 1 runterrechnen
		if (x == 0 && y == 0) {
			throw new IllegalArgumentException("Ungültige Richtung angegeben!");
		}
		
		if (x > 1 || x < -1 || y > 1 || y < -1) {
			if (Math.abs(x) > Math.abs(y)) {
				y = Math.round(1f*y/Math.abs(x));
				x /= Math.abs(x); 
			}
			else {
				x = Math.round(1f*x/Math.abs(y));
				y /= Math.abs(y); 
			}
		}
		
		if (x != 0 || y != 0) {
			switch (y) {
			case -1:
				switch (x) {
				case 0:
					return NORTH;
				case 1:
					return NORTHEAST;
				case -1:
					return NORTHWEST;
				}
			case 1:
				switch (x) {
				case 0:
					return SOUTH;
				case 1:
					return SOUTHEAST;
				case -1:
					return SOUTHWEST;
				}
			default:
				switch (x) {
				case 1:
					return EAST;
				case -1:
					return WEST;
				}
			}
		}
		
		throw new IllegalArgumentException("Ungültigen Wert berechnet!");
	}

	public static Point getDirection(int dir) {
		switch (dir) {
		case NORTH:
			return new Point(0, -1);
		case NORTHEAST:
			return new Point(1, -1);
		case EAST:
			return new Point(1, 0);
		case SOUTHEAST:
			return new Point(1, 1);
		case SOUTH:
			return new Point(0, 1);
		case SOUTHWEST:
			return new Point(-1, 1);
		case WEST:
			return new Point(-1, 0);
		case NORTHWEST:
			return new Point(-1, -1);
		}
		throw new IllegalArgumentException("Ungültige Richtung "+dir+" angegeben!");
	}
}
